package com.xb.model.domain.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.xb.model.domain.dto.PackageDetailsDTO;
import lombok.Data;
import java.time.LocalDateTime;
import com.fasterxml.jackson.databind.ObjectMapper;
@Data
@TableName("recharge_record")
public class RechargeRecord {
    @TableId(type = IdType.AUTO)
    private Long id;

    private Long userId;

    private String phone;

    private Integer amount;

    // 直接映射为 String 类型，不使用 TypeHandler
    @TableField("package_details")
    private String packageDetailsJson;

    // 非数据库字段，用于返回解析后的对象
    @TableField(exist = false)
    private PackageDetailsDTO packageDetails;

    private LocalDateTime transferTime;

    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    @TableLogic
    private Integer isDeleted;

    // 手动解析 packageDetailsJson 到 packageDetails
    public PackageDetailsDTO getPackageDetails() {
        if (packageDetailsJson != null && !packageDetailsJson.isEmpty()) {
            try {
                ObjectMapper mapper = new ObjectMapper();
                return mapper.readValue(packageDetailsJson, PackageDetailsDTO.class);
            } catch (Exception e) {
                e.printStackTrace(); // 可以替换为日志
            }
        }
        return null;
    }
}